package com.base.mine.presenter;

import android.app.Activity;
import android.text.TextUtils;


import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.utils.SystemUtil;
import com.base.mine.R;
import com.base.mine.bean.TaskInfo;
import com.base.mine.bean.Version;
import com.base.mine.view.VersionView;

import java.io.File;
import java.util.HashMap;

import io.reactivex.disposables.Disposable;

/**
 * Created by weikailiang on 2019/5/6.
 */

public class VersionPresenter extends BasePresenter<VersionView> {
    public VersionPresenter(VersionView view, Activity activity) {
        super(view,activity);
    }

    @Override
    public void start() {

    }
    public void CheckVersion() {
        mView.showPb();
        HashMap<String, String> param = new HashMap<>();
        param.put("appVersion", SystemUtil.packageName(mActivity));
        param.put("versionCode", SystemUtil.getVersionCode(mActivity)+"");
        param.put("appName", mActivity.getString(R.string.app_name));
        Disposable disposable = RealNetWorkUtil.postContent()
                .method("api-basic/app/check")
                .content(param)
                .build().enqueue(Version.class, new CallBackImpl<Version>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb();
                        if (!TextUtils.isEmpty(msg)) {
                            mView.showMes(msg);
                        }
                    }

                    @Override
                    public void onNext(Version bean) {
                        mView.hidePb();
                        if (bean.isHaveNewVersion()){
                            if (TextUtils.isEmpty(bean.getUrl())){
                                mView.showMes("下载地址有误，请联系管理员修改");
                                return;
                            }
                            File file = new File(SystemUtil.getAppDir());
                            if (!file.exists()){
                                file.mkdirs();
                            }
                            File file1 = new File(SystemUtil.getAppDir()+bean.getAppName());
                            if (file1.exists()){
                                file1.delete();
                            }
                            TaskInfo info = new TaskInfo();
                            info.setUrl(bean.getUrl());
                            info.setPath(SystemUtil.getAppDir());
                            info.setName(bean.getAppName());
                            info.setRemark(bean.getDescription());
                            mView.hasNewVersion(info);
                        }else {
                            mView.showMes("当前版本为最新版本");
                        }
                    }
                });
        mCompositeDisposable.add(disposable);
    }
}
